package com.ljye.jg.ljye.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ljye.jg.ljye.R;
import com.ljye.jg.ljye.utils.CommonViewHolder;

import java.util.ArrayList;
import java.util.HashMap;

public class Activity_Roll_Call extends Activity {

    private static CheckBox cball;//全选
    private static MyAdpapter mLvAdapter;
    private static Button bt;//确定
    private ImageView back;//返回
    private ImageView iv;//图片
    private TextView tvTitle;//课程标题
    private TextView tvAddress;//地址
    private TextView tvTime;//时间
    private ListView listview;
    private ArrayList<String> listData;
    public View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.back://返回
                    finish();
                    break;
                case R.id.bt://确定
                    HashMap<Integer, Boolean> isSelected = MyAdpapter.getIsSelected();
                    for (int i = 0; i < listData.size(); i++) {
                        Boolean aBoolean = isSelected.get(i);
                        String s = listData.get(i);
                        Log.i("Activity_Roll_Call", s + ":" + aBoolean);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__roll__call);
        initView();

        back.setOnClickListener(listener);
        bt.setOnClickListener(listener);
        //上一个界面传过来的date
//            Intent intent = getIntent();
//            Bundle extras = intent.getExtras();
//            int aClass = extras.getInt("class");
        initlistview();

    }

    private void initlistview() {
        listData = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            listData.add("习近平" + i);
        }

        mLvAdapter = new MyAdpapter(this, listData);
        listview.setAdapter(mLvAdapter);
        setListViewHeight(listview);
    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        iv = (ImageView) findViewById(R.id.iv);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvAddress = (TextView) findViewById(R.id.tv_address);
        tvTime = (TextView) findViewById(R.id.tv_time);
        cball = (CheckBox) findViewById(R.id.cb);
        listview = (ListView) findViewById(R.id.lv_ew);


        bt = (Button) findViewById(R.id.bt);

    }

    //解决ScrollView包裹listview后只显示一个条目问题
    public void setListViewHeight(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }


    public static class MyAdpapter extends BaseAdapter {
        private static HashMap<Integer, Boolean> isSelected;
        private Context context;
        private ArrayList<String> listDate; // 用来控制CheckBox的选中状况

        public MyAdpapter(Context context, ArrayList<String> listDate) {
            this.listDate = listDate;
            this.context = context;
            isSelected = new HashMap<Integer, Boolean>();
            // 初始化数据
            initDate();
        }

        public static HashMap<Integer, Boolean> getIsSelected() {
            return isSelected;
        }

        public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
            MyAdpapter.isSelected = isSelected;
        }

        @Override
        public int getCount() {
            return listDate == null ? 0 : listDate.size();
        }

        private void initDate() {
            for (int i = 0; i < listDate.size(); i++) {
                getIsSelected().put(i, false);
            }
        }

        @Override
        public Object getItem(int i) {
            return listDate.get(i);
        }

        @Override
        public long getItemId(int i) {
            return listDate.size();
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {

            CommonViewHolder holder = CommonViewHolder.createCVH(view, viewGroup, R.layout.listview_dian);

            TextView name = holder.getTv(R.id.name);//姓名
            TextView leave = holder.getTv(R.id.leave);//请假
            final CheckBox cb = holder.getCb(R.id.cb_item);//是否勾选
            name.setText(listDate.get(i));
            leave.setVisibility((i == 1) ? (View.VISIBLE) : (View.INVISIBLE));
            cball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b) {
                        for (int i1 = 0; i1 < listDate.size(); i1++) {
                            isSelected.put(i1, true);
                            setIsSelected(isSelected);
                        }
                        mLvAdapter.notifyDataSetChanged();
                    } else {
                        for (int i1 = 0; i1 < listDate.size(); i1++) {
                            isSelected.put(i1, false);
                            setIsSelected(isSelected);
                        }
                        mLvAdapter.notifyDataSetChanged();
                    }
                }
            });

            cb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isSelected.get(i)) {
                        isSelected.put(i, false);
                        setIsSelected(isSelected);
                    } else {
                        isSelected.put(i, true);
                        setIsSelected(isSelected);
                    }
                }
            });


            cb.setChecked(getIsSelected().get(i));
            return holder.convertView;
        }

    }

}